import request from './index';
import { getServerUrl } from '../utils';

/**
 * 普通web上传
 * @param file
 */
export function uploadNormal(file: File): Promise<Record<string, any>> {
  const formData = new FormData();
  formData.append('file', file);
  return request.post('/file/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data;charset=UTF-8',
    },
  });
}

/**
 * 调用原生上传
 * @param nativePath 原生路径
 */
export function uploadNative(nativePath: string, onProgress: Function) {
  console.log('nativePath', nativePath);
  return new Promise<BaseObject>((resolve, reject) => {
    window.HWH5.uploadImage({
      zip: false,
      serverUrl: `${getServerUrl()}/file/upload`, // 示例地址，非真实服务
      filePath: nativePath,
      formData: {},
      timeout: 60000,
      progress: 1,
      onProgress,
    }).then((data) => {
      resolve(data);
      console.log('服务端返回数据：', data);
    }).catch((error) => {
      reject(error);
      console.log('上传异常', error);
    });
  });
}
export default {};
